import { Component, OnInit } from '@angular/core';
import { PlatformuserService } from '../../service/platformuser.service';
import { RoleService } from '../../service/role.service';
import { UserService } from '../../service/user.service';
import { CommonService } from '../../service/common.service';
import { CityService } from '../../service/city.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-sysuerlist',
  templateUrl: './sysuerlist.component.html',
  styleUrls: ['./sysuerlist.component.scss'],
  providers: [PlatformuserService, RoleService, UserService, CityService]
})
export class SysuerlistComponent implements OnInit {
  relatephone: any;
  name: any;
  role: any;
  pageIndex = 1;
  pageSize = 10;
  total = 1;
  dataSet = [];
  loading = true;
  isOkLoading = false;
  isVisible = false;
  roleItems: Array<any> = []
  doc: any = {};
  status: any;
  cityItems = [{ lable: '省会城市', value: 1 }, { lable: '地级市', value: 2 }, { lable: '县级市', value: 3 }];
  statuslist = [{ key: null, value: '全部' }, { key: 1, value: '有效' }, { key: 0, value: '禁用' }];
  areas: Array<any> = [];
  nzOptions: Array<any> = [];
  isshow = false;

  // tslint:disable-next-line:max-line-length
  constructor(private customeruserservice: UserService, private userservice: PlatformuserService, private roleservice: RoleService, private msg: NzMessageService, private commonservice: CommonService, private cityservice: CityService) {
    this.cityservice.querys().then((res) => {
      this.nzOptions = res;
    });
  }

  ngOnInit() {
    this.roleservice.query().then((res) => {
      this.roleItems = res;
    });

    this.searchData();
  }

  searchData(reset: boolean = false): void {
    this.loading = true;
    this.userservice.query({
      offset: this.pageIndex,
      limit: this.pageSize,
      name: this.name,
      status: this.status,
      role: this.role
    }).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }

  handleOk(): void {
    this.isOkLoading = true;
    if (!this.doc.name || this.doc.name === '') {
      this.msg.warning('请填写用户名！');
    } else if (!this.doc.roleId || this.doc.roleId.length === 0) {
      this.msg.warning('请设置角色！');
    } else if (!this.doc.phone || this.doc.phone === '') {
      this.msg.warning('请填写手机号！');
      // } else if (!this.doc.password || this.doc.password === '') {
      //   this.msg.warning('请填写密码！');
      // } else if (this.doc.password.length < 6) {
      //   this.msg.warning('密码长度不低于6位！');
    } else if (this.isshow && !this.doc.relatephone) {
      this.msg.warning('请设置品牌宣传大使关联小程序手机号');
    } else if (this.areas.length < 2) {
      this.msg.warning('请选择所在城市');
    } else if (!this.doc.citytype) {
      this.msg.warning('请选择城市级别');
    } else {
      if (this.areas.length === 2) {
        this.doc.province = this.areas[0];
        this.doc.city = this.areas[1];
      }
      if (this.areas.length === 3) {
        this.doc.province = this.areas[0];
        this.doc.city = this.areas[1];
        this.doc.district = this.areas[2];
      }
      this.userservice.update(this.doc).then((res) => {
        this.searchData();
        if (this.doc._id) {
          this.msg.success('修改成功！');
        } else {
          this.msg.success('新增成功！');
        }
        this.isVisible = false;
        this.doc = {};
        this.areas = [];
      });
    }

  }


  handleCancel(): void {
    this.isVisible = false;
    this.doc = {};
    this.relatephone = '';
  }

  /**新增 */
  add() {
    this.isVisible = true;
  }

  /**修改 */
  edit(id: any) {
    this.userservice.queryById(id).then((res) => {
      this.areas = [];
      if (res.province && res.province !== '') {
        this.areas.push(res.province)
      }
      if (res.city && res.city !== '') {
        this.areas.push(res.city)
      }
      if (res.district && res.district !== '') {
        this.areas.push(res.district)
      }
      // if(res.customerid){
      //   this.customeruserservice.queryoneuser({_id:res.customerid}).then((res)=>{
      //     this.relatephone=res.phone;
      //    });

      // }
      this.doc = res;
      const role = this.roleItems.find((a) => {
        return this.doc.roleId.findIndex((b) => {
          return b === a._id;
        }) >= 0;
      })
      if ((role && role.name.indexOf('大使') >= 0) || (role && role.name.indexOf('餐厅') >= 0)) {
        this.isshow = true;
      } else {
        this.isshow = false;
      }
      this.isVisible = true;
    });
  }
  /**禁用 */
  lock(id: any) {
    this.commonservice.confirm('确定要禁用用户吗?', () => {
      this.userservice.lock(id).then((res) => {
        this.msg.success('用户已禁用！');
        this.searchData();
      });
    })
  }
  /**启用 */
  open(id: any) {
    this.commonservice.confirm('确定要启用用户吗?', () => {
      this.userservice.open(id).then((res) => {
        this.msg.success('用户已启用！');
        this.searchData();
      });
    })
  }
  onChanges(values: any): void {
    console.log(values, this.areas);
  }
  checkrole() {
    const role = this.roleItems.find((a) => {
      return a._id === this.doc.roleId;
    })
    if ((role && role.name.indexOf('大使') >= 0) || (role && role.name.indexOf('餐厅') >= 0)) {
      this.isshow = true;
    } else {
      this.isshow = false;
      delete this.doc.relatephone;
    }
  }

  getpassword() {
    if (this.doc.phone) {
      this.doc.password = this.doc.phone.substr(this.doc.phone.length - 6, this.doc.phone.length);
    }

  }
}
